package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.project.common.Result;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.CameraData;
import edu.ynu.se.xiecheng.achitectureclass.project.service.CameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/camera")
public class CameraController {

    @Autowired
    private CameraService cameraService;

    /**
     * 抓拍摄像头照片并存储到数据库
     *
     * @param deviceSerial 摄像头设备序列号
     * @param quality      视频清晰度
     * @return 存储的CameraData实体
     */
    @PostMapping("/capture")
    public Result<CameraData> captureAndSavePhoto(
            @RequestParam String deviceSerial,
            @RequestParam int quality) {
        try {
            CameraData cameraData = cameraService.captureAndSavePhoto(deviceSerial, quality);
            return Result.success(cameraData);
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 根据serialId删除数据
     *
     * @param serialId 摄像头设备序列号
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteBySerialId(@RequestParam String serialId) {
        try {
            cameraService.deleteBySerialId(serialId);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 查询所有数据
     *
     * @return 所有CameraData实体
     */
    @GetMapping("/all")
    public Result<List<CameraData>> findAll() {
        try {
            List<CameraData> cameraDataList = cameraService.findAll();
            return Result.success(cameraDataList);
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 手动导入图片数据（支持 JSON 请求体）
     *
     * @param requestBody JSON 请求体，包含 deviceSerial 和 imageUrl
     * @return 存储的CameraData实体
     */
    @PostMapping("/import")
    public Result<CameraData> importPhoto(@RequestBody Map<String, String> requestBody) {
        try {
            String deviceSerial = requestBody.get("deviceSerial");
            String imageUrl = requestBody.get("imageUrl");

            // 检查参数是否为空
            if (deviceSerial == null || imageUrl == null) {
                return Result.error(400, "deviceSerial 或 imageUrl 不能为空");
            }

            CameraData cameraData = cameraService.importPhoto(deviceSerial, imageUrl);
            return Result.success(cameraData);
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 查询最新的图片数据
     *
     * @return 最新的CameraData实体
     */
    @GetMapping("/latest")
    public Result<CameraData> getLatestPhoto() {
        try {
            CameraData latestPhoto = cameraService.getLatestPhoto();
            return Result.success(latestPhoto);
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }
}